package com.example.zhu95.mytimber.activity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.MenuItem;

import com.example.zhu95.mytimber.MyApplication;
import com.example.zhu95.mytimber.PlayMusicService;
import com.example.zhu95.mytimber.R;
import com.example.zhu95.mytimber.entity.PlayQueueRecord;
import com.example.zhu95.mytimber.listener.OnAllRandomPlayClickListener;
import com.example.zhu95.mytimber.receiver.MusicStateReceiver;

import jp.co.recruit_lifestyle.android.widget.PlayPauseButton;

import static com.example.zhu95.mytimber.ConstantValue.MUSIC_STATE_RECEIVE;
import static com.example.zhu95.mytimber.ConstantValue.MusicState_Paused;
import static com.example.zhu95.mytimber.ConstantValue.MusicState_Playing;

/**
 * @author zhu95
 * @date 2018/3/13 0:13
 */
public abstract class BaseActivity extends AppCompatActivity {
    private boolean notifyUpdateUI = false;
    public static int songid = -1;
    public static int currentPosition = 0;
    public static String state = MusicState_Paused;
    protected MusicStateReceiver musicStateReceiver;

    protected ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
 /*           playMusicBinder = (PlayMusicService.PlayMusicBinder) service;

            if (!(BaseActivity.this instanceof NowPlayingActivity)) {
                updateUIviaMusicService(BaseActivity.this);
            }*/
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };

    public boolean isNotifyUpdateUI() {
        return notifyUpdateUI;
    }

    public void setNotifyUpdateUI(boolean notifyUpdateUI) {
        this.notifyUpdateUI = notifyUpdateUI;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        songid = PlayQueueRecord.Companion.getCurrentSongIdFromDB();
        currentPosition = PlayQueueRecord.Companion.getCurrentPositionFromDB();
        Intent startServiceIntent = new Intent(this, PlayMusicService.class);
        startService(startServiceIntent);
        bindService(startServiceIntent, serviceConnection, BIND_AUTO_CREATE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (musicStateReceiver == null) {
            musicStateReceiver = new MusicStateReceiver();
            musicStateReceiver.setActivity(this);
            IntentFilter filter = new IntentFilter(MUSIC_STATE_RECEIVE);
            MyApplication.getLocalBroadcastManager().registerReceiver(musicStateReceiver, filter);
        }
        updateUI(songid, state, currentPosition);
        notifyUpdateUI = true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (musicStateReceiver != null) {
            MyApplication.getLocalBroadcastManager().unregisterReceiver(musicStateReceiver);
        }
        musicStateReceiver = null;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_settings: {
                Intent intent = new Intent(this, SettingsActivity.class);
                startActivity(intent);
                break;
            }
            case R.id.action_all_random:
                new OnAllRandomPlayClickListener().onClick(null);
                break;
            case R.id.action_search: {
                Intent intent = new Intent(this, SearchActivity.class);
                startActivity(intent);
                break;
            }
            default:
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(serviceConnection);
    }

    public static void updatePlayPauseBtnState(PlayPauseButton btnPlayPause, String state) {
        if (TextUtils.equals(state, MusicState_Playing) && !btnPlayPause.isPlayed()) {
            btnPlayPause.setPlayed(true);
            btnPlayPause.startAnimation();
        } else if ((!TextUtils.equals(state, MusicState_Playing)) && btnPlayPause.isPlayed()) {
            btnPlayPause.setPlayed(false);
            btnPlayPause.startAnimation();
        }
    }

    public void updateUI(int songid, String musicState, int currentPosition) {
        BaseActivity.songid = songid;
        BaseActivity.state = musicState;
        BaseActivity.currentPosition = currentPosition;
    }

    public void updateProgress(int songid, int currentPosition) {
        BaseActivity.currentPosition = currentPosition;
    }
}

